<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        .box {
            width: 400px;
            height: 300px;
            border: solid 1px black;
            margin: 0 auto;
        }

        .box ul {
            margin: 0;
            padding: 0;
            list-style: none;
            display: flex;
            background: #eee;
            height: 30px;
            line-height: 30px;
        }

        .box ul li {
            flex: 1;
            border-left: solid 1px #fff;
            border-right: solid 1px #fff;
            text-align: center;
            cursor: pointer;
        }

        .box ul li.active {
            background: red;
            color: #fff;
        }

        .box .cont > div {
            height: 270px;
            display: none;
        }

        .phone {
            background: #399;
        }

        .computer {
            background: #393;
        }

        .bike {
            background: #939;
        }
    </style>

</head>
<body>
    <div class="box">
        <ul>
            <li>手机</li>
            <li>电脑</li>
            <li>自行车</li>
        </ul>
        <div class="cont">
            <div class="phone">这是手机</div>
            <div class="computer">这是电脑</div>
            <div class="bike">这是自行车</div>
        </div>
    </div>
</body>
<script>

    var ali = document.querySelectorAll(".box ul li");
    var acont = document.querySelectorAll(".cont > div");

    var index = 0;

    ali[index].className = "active";
    acont[index].style.display = "block";

    for (let i = 0; i < ali.length ; i++) {
        ali[i].index = i;
        ali[i].onclick = function () {
            ali[index].className = "";
            acont[index].style.display = "none"
            this.className = "active";
            acont[this.index].style.display = "block";
            index = this.index;
        };

    }

</script>
</html>



















